/*
 * $Id: RequestDUI.java,v 1.9 2009/12/14 20:29:03 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.ui.dao;

import java.util.ArrayList;
import java.util.List;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.HstStepTime;
import com.ge.healthcare.autosc.ui.pojo.UiFileDetails;
import com.ge.healthcare.autosc.ui.pojo.UiRequest;
import com.ge.healthcare.autosc.ui.pojo.UiStepLog;

public class RequestDUI extends BaseDaoUI {
	public static UserInterfaceDAO uiDAO;
	public static UiRequest getRequest(String requestId) {
		System.out.println("Entering Get Request");
		UiRequest request;
		try {
			requestId = cleanupInputString(requestId);
			request = uiDAO.getRequest(requestId);
			if (ASCUtil.isNullOrEmptyObject(request)
					|| ASCUtil.isNullOrEmptyObject(request.getRequestId())) {
				String msg = "No Record for Request Id: " + requestId;
				throw new RuntimeException(msg);
			}
		} catch (Exception e) {
			request = new UiRequest();
			setErrorMessage(request, e);
		}
		return request;
	}

	public static List<UiFileDetails> getRequestFiles(String requestId) {
		List<UiFileDetails> files;
		try {
			requestId = cleanupInputString(requestId);
			files = uiDAO.getRequestFiles(requestId);
			if (ASCUtil.isNullOrEmptyList(files) || files.isEmpty()
					|| files.size() < 1) {
				String msg = "No File Records for Request Id: " + requestId;
				files = new ArrayList<UiFileDetails>();
				UiFileDetails file = new UiFileDetails();
				setErrorMessage(file, msg);
				files.add(file);
			}
		} catch (Exception e) {
			files = new ArrayList<UiFileDetails>();
			UiFileDetails file = new UiFileDetails();
			setErrorMessage(file, e);
			files.add(file);
		}
		return files;
	}

	public static List<UiStepLog> getRequestStepLogs(String stepId) {
		List<UiStepLog> list;
		try {
			list = RequestDUI.uiDAO.getStepLogs(HstStepTime.REQUEST_TYPE
					.toString(), stepId);
			if (ASCUtil.isNullOrEmptyList(list) || list.isEmpty()
					|| list.size() < 1) {
				String msg = "No Records for step Id: " + stepId;
				throw new RuntimeException(msg);
			}
		} catch (Exception e) {
			list = new ArrayList<UiStepLog>();
			UiStepLog file = new UiStepLog();
			setErrorMessage(file, e);
			list.add(file);
		}
		return list;
	}

	public static List<UiStepLog> getTaskStepLogs(String stepId) {
		List<UiStepLog> list;
		try {
			list = RequestDUI.uiDAO.getStepLogs(HstStepTime.TASK_TYPE
					.toString(), stepId);
			if (ASCUtil.isNullOrEmptyList(list) || list.isEmpty()
					|| list.size() < 1) {
				String msg = "No Records for step Id: " + stepId;
				throw new RuntimeException(msg);
			}
		} catch (Exception e) {
			list = new ArrayList<UiStepLog>();
			UiStepLog file = new UiStepLog();
			setErrorMessage(file, e);
			list.add(file);
		}
		return list;
	}

	public void setUiDAO(UserInterfaceDAO uiDAO) {
		RequestDUI.uiDAO = uiDAO;
	}
}
